<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { updateMp } from "@/utils/validate";
import themeList from "@/utils/theme";
// 创建 WebSocket 工具类--start--
import WebsocketUtil from "@/utils/webSocket.ts";
const websocketUtil = new WebsocketUtil(); // 创建 WebSocket 工具类实例
provide("websocketUtil", websocketUtil); // 提供给所有子组件
// 创建 WebSocket 工具类--end--

// 定义全局数据--start--
const globalData = reactive({
  pid: "",
});
provide("globalData", globalData); // 提供全局数据给所有组件使用
// 定义全局数据--end--

onLaunch(() => {
  const queryData = uni.getEnterOptionsSync(); // uni-app版本 3.5.1+ 支持
  // 合并各种方式获取的参数
  const urlParams = parseUrlParams();

  const finalQuery = {
    ...queryData.query,
    ...urlParams,
  };
  if (finalQuery.pid) {
    globalData.pid = finalQuery.pid;
  }

  // #ifdef MP
  updateMp();
  // #endif
});
onShow(() => {
  console.log("App Show");
});
onHide(() => {
  console.log("App Hide");
});

const selectedTheme = themeList["red"];
uni.setStorageSync("viewColor", selectedTheme);

/**
 * 解析URL参数
 * @returns {Object} 解析后的参数对象
 */
function parseUrlParams() {
  const params: any = {};

  // #ifdef H5
  // 解析history模式下的URL参数
  if (window.location.search) {
    const urlParams = new URLSearchParams(window.location.search);
    for (const [key, value] of urlParams) {
      params[key] = value;
    }
  }

  // 解析hash模式下的参数（如果存在）
  if (window.location.hash.includes("?")) {
    const hashParams = window.location.hash.split("?")[1];
    if (hashParams) {
      const hashSearchParams = new URLSearchParams(hashParams);
      for (const [key, value] of hashSearchParams) {
        params[key] = value;
      }
    }
  }
  // #endif

  return params;
}
</script>

<style lang="scss">
@import "uview-plus/index.scss";
@import "@/static/css/base.css";
@import "@/static/css/flex.scss";
</style>
